package com.hissy.biz;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.hissy.common.BeanCopyUtil;
import com.hissy.dao.dto.EmpRolePo;
import com.hissy.dao.dto.EmpRolePoExample;
import com.hissy.dao.dto.MenuPo;
import com.hissy.dao.dto.MenuPoExample;
import com.hissy.dao.dto.RoleMenuPo;
import com.hissy.dao.dto.RoleMenuPoExample;
import com.hissy.dao.dto.RolePo;
import com.hissy.dao.generatedMapper.EmpRolePoMapper;
import com.hissy.dao.generatedMapper.EmployeePoMapper;
import com.hissy.dao.generatedMapper.MenuPoMapper;
import com.hissy.dao.generatedMapper.RoleMenuPoMapper;
import com.hissy.entity.emp.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by zhangyanfei on 2018/9/12.
 *
 * @author zhangyanfei
 * @date 2018/09/12
 */
@Service
public class EmpBiz {
    @Autowired
    private EmpRolePoMapper empRolePoMapper;
    @Autowired
    private RoleMenuPoMapper roleMenuPoMapper;
    @Autowired
    private MenuPoMapper menuPoMapper;

    public MenuVo getMenuInfoByEmpId(Long empId) {
        EmpRolePoExample condition = new EmpRolePoExample();
        condition.createCriteria().andEmpIdEqualTo(empId);
        List<EmpRolePo> rolePos = empRolePoMapper.selectByExample(condition);
        List<Long> roleIds = rolePos
            .stream()
            .map(EmpRolePo::getRoleId)
            .collect(Collectors.toList());
        RoleMenuPoExample roleMenuPoExample = new RoleMenuPoExample();
        roleMenuPoExample.createCriteria().andRoleIdIn(roleIds);
        List<RoleMenuPo> roleMenuPos = roleMenuPoMapper.selectByExample(roleMenuPoExample);
        List<Long> menuIds = roleMenuPos.stream().map(RoleMenuPo::getMenuId).collect(Collectors.toList());
        MenuPoExample menuPoExample = new MenuPoExample();
        menuPoExample.createCriteria().andIdIn(menuIds);
        //拿到菜单
        List<MenuPo> menuPos = menuPoMapper.selectByExample(menuPoExample);

        List<MenuVo> menuVos = menuPos.stream().map(menuPo -> {
            MenuVo menuVo = new MenuVo();
            BeanCopyUtil.copyProps(menuPo, menuVo);
            return menuVo;
        }).collect(Collectors.toList());

        MenuVo rootMenu = new MenuVo();
        // 根menuId 从0开始
        rootMenu.setId(0L);
        rootMenu.setMenuName("root-menu");
        // 生成子菜单集合并存入菜单
        rootMenu.setChildren(this.filterChildrenMenuByParentId(rootMenu.getId(), menuVos));
        return rootMenu;
    }

    /**
     * 递归调用
     *
     * @param parentId
     * @param menuVos
     * @return
     */
    private List<MenuVo> filterChildrenMenuByParentId(long parentId, List<MenuVo> menuVos) {
        // 一个菜单只存在一个parentId
        List<MenuVo> newMenuList = new ArrayList<>();
        for (MenuVo menuVo : menuVos) {
            // 避免重复插入
            if (menuVo.getParentId() == parentId && !newMenuList.contains(menuVo)) {
                MenuVo menu = new MenuVo();
                menu.setId(menuVo.getId());
                menu.setMenuNo(menuVo.getMenuNo());
                menu.setMenuName(menuVo.getMenuName());
                menu.setUrl(menuVo.getUrl());
                if (menuVo.getUrl().contains("http")) {
                    menu.setUrl(menuVo.getUrl());
                }
                menu.setDisplaySeq(menuVo.getDisplaySeq());
                menu.setChildren(this.filterChildrenMenuByParentId(menuVo.getId(), menuVos));
                //子菜单
                newMenuList.add(menu);
            }
        }

        Collections.sort(newMenuList);
        return newMenuList;
    }
}
